<?php
/**
 * Created by PhpStorm.
 * User: 万奇
 * Date: 2021/2/4 0004
 * Time: 11:31
 */

namespace app\api\controller;

use app\core\BaseController;
use think\App;
use think\facade\Db;
use app\Request;

class Content extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 内容列表
     * User: 李宁
     * */
    public function get_content_list(){
        $param = $this->request->post();
        $where = [];
        $type = $param['type'];
        if($type){
            $where[] = ['type','=',$type];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['title|content','like','%'.$param['keyword'].'%'];
        }
        if(isset($param['industry']) && !empty($param['industry'])){
            $where[] = ['industry','=',$param['industry']];
        }
        $type_arr = [1=>'知识库',2=>'案例库',3=>'快捷消息'];
        $industry = [
            1 => '护肤彩妆',
            2 => '教育',
            3 => '旅游',
            4 => '大交通',
            5 => '房产',
            6 => '商务服务',
            7 => '本地生活',
            8 => '医疗',
            9 => '金融',
            10 => '摄影',
            11 => '海外保健品',
            12 => '招商加盟',
            13 => '家具',
            14 => '零售百货',
            15 => '家居',
            16 => '健康',
            17 => '文体娱乐'
        ];
        $list = Db::name('cms_content_engine')
            ->field('id,title,cover,industry,content,type,create_at')
            ->where($where)
            ->order('create_at' , 'desc')
            ->paginate($param['limit'])
            ->each(function ($item) use ($type_arr,$industry) {
                $item['type'] = $type_arr[$item['type']];
                $item['desc'] = mb_substr(strip_tags($item['content']),0,50,'utf-8');
                $item['industry'] = array_key_exists($item['industry'],$industry) ? $industry[$item['industry']] : '';
                unset($item['content']);
                return $item;
            })->toArray();

        response(200, '', $list['data'], $list['total']);
    }
    /**
     * 内容详情
     * User: 李宁
     * */
    public function get_content_detail()
    {
        $param = $this->request->post();
        $id = $param['id'];
        if(!$id){
            response(500, '缺少id');
        }
        $data = Db::name('cms_content_engine')
            ->field('id,title,cover,industry,content,type,create_at')
            ->where('id',$id)->find();

        response(200, '',$data);
    }

    /**
     * 案例库分类
     * User: 李宁
     * */
    public function get_case_category(){
        $data = [
            1 => '护肤彩妆',
            2 => '教育',
            3 => '旅游',
            4 => '大交通',
            5 => '房产',
            6 => '商务服务',
            7 => '本地生活',
            8 => '医疗',
            9 => '金融',
            10 => '摄影',
            11 => '海外保健品',
            12 => '招商加盟',
            13 => '家具',
            14 => '零售百货',
            15 => '家居',
            16 => '健康',
            17 => '文体娱乐'
        ];
        response(200, '',$data);
    }

}